//
//  elaUSCrudeOilFieldProduction.m
//  MarketDataColloctor
//
//  Created by jiro on 10/10/17.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "eiaUSCrudeOilFieldProduction.h"
#import "eiaHtmlParser.h"
#import "WebResourceCollector.h"
#import "../../../Commons/DbOperator.h"
#import "../../../DbTypes/DbUtility.h"

@implementation eiaUSCrudeOilFieldProduction

+(void) collect
{
	//	get html
	NSError* error;
	NSURL* url = [ NSURL URLWithString: 
		@"http://www.eia.gov/dnav/pet/hist/LeafHandler.ashx?n=PET&s=WCRFPUS2&f=W" ];
	id urls = [ NSArray arrayWithObject: url ];
	id htmls = [ NSMutableArray array ];
	id html = nil;
//	id html = [ NSString stringWithContentsOfFile: @"/ela.html" encoding: NSUTF8StringEncoding error: & error ];

	if ( html == nil ) {
		WebResourceCollector* collector = [ [ WebResourceCollector alloc ] init ];
		[ collector collectContentsOf: urls intoStrings: htmls ];
		
		id html = [ htmls objectAtIndex: 0 ];
		[ html writeToFile: @"/ela.html" atomically: YES encoding: NSUTF8StringEncoding error: & error ];
	}
	//NSLog( @"%@", html );

	//	---- get latest data from db
	id dbOperator = [ DbOperator defaultOperator ];
	id sql = @"select date from us_crude_oil_field_production_table order by date desc limit 1";
	PGresult* res;
	if ( DbResponse_Success != [ dbOperator executeQuery: sql intoResult: & res ] ) {
		NSLog( @"sql failed, sql: %@, message: %@", sql, [ dbOperator errorMessage ] );
	}
	const char* s = nil;
	if ( PQntuples( res ) != 0 ) {
		s = PQgetvalue( res, 0, 0 );
	}
	NSDate* latestDate = nil;
	if ( s != nil ) {
		latestDate = [ DbUtility parseAsNSDate: s ];
	}
	// ----

	id results = [ NSMutableDictionary dictionary ];
	[ EiaHtmlParser parseDataHtml: html dataInterval: Weekly latestDate: latestDate result: results ];
	

	//	create sql query
	bool valueAdded = NO;
	sql = @"insert into us_crude_oil_field_production_table values\n";
	NSEnumerator* e = [ results keyEnumerator ];
	NSDate* date;
	while ( date = [ e nextObject ] ) {
		if ( s != nil && [ date timeIntervalSinceDate: latestDate ] <= 0 ) {
			continue;
		}
		if ( valueAdded ) {
			sql = [ sql stringByAppendingString: @", " ];
		} else {
			valueAdded = YES;
		}
		sql = [ sql stringByAppendingFormat: @"( '%@', %@ )\n", date, [ results objectForKey: date ] ];
	}
	sql = [ sql stringByAppendingString: @";" ];
	NSLog( @"%@", sql );
	//	 ----

	//	 ---- insert data to table 
	if ( DbResponse_Success != [ dbOperator executeQuery: sql intoResult: & res ] ) {
		NSLog( @"sql failed, message: %@, sql: %@", [ dbOperator errorMessage ], sql );
	}
	//	----

	[ dbOperator disconnect ];

}

@end
